/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iffmyexp;

import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;

/**
 *
 * @author me
 */
public class workflowDetails {

    /*static search[] wfResArray = Page1.wfSearch;
    static search wfRes = wfResArray[Page1.workflowsTable.getRowIndex()];
    static String uri = wfRes.user;*/
    static String desc = "";
    static inputModel[] inputs;
    static inputModel currInput;
    static String thumb = "";
    static int count = -1;

    public static void getDetails() {

        search[] wfResArray = Page1.wfSearch;
        search wfRes = wfResArray[Page1.workflowsTable.getRowIndex()];
        String uri = wfRes.user;


        try {
            DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = Factory.newDocumentBuilder();
            Document doc = builder.parse(uri + "&all_elements=yes");
            //creating an XPathFactory:
            XPathFactory factory = XPathFactory.newInstance();
            //using this factory to create an XPath object:
            XPath xpath = factory.newXPath();
            //XPath object created compiles the XPath expression for workflows:
            XPathExpression descExpr = xpath.compile("//description/text()");
            Object descResult = descExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList descNodeList = (NodeList) descResult;

            XPathExpression inputExpr = xpath.compile("//components/sources/source/name/text()");
            Object inputResult = inputExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList inputNodeList = (NodeList) inputResult;

            XPathExpression thumbExpr = xpath.compile("//thumbnail-big/text()");
            Object thumbResult = thumbExpr.evaluate(doc, XPathConstants.NODESET);
            NodeList thumbNodeList = (NodeList) thumbResult;


            //set workflow variable values from xpath expressions
            desc = descNodeList.item(0).getNodeValue();

            inputs = new inputModel[inputNodeList.getLength()];
            for (int i = 0; i < inputNodeList.getLength(); i++) {
                inputModel inputName = new inputModel(inputNodeList.item(i).getNodeValue(), "a", "b", "c");

                inputs[i] = inputName;
            }

            thumb = thumbNodeList.item(0).getNodeValue();

        } catch (Exception e) {
        }

    //currInput = inputs[0];

    }

    public static void resetCount() {
        count = -1;
    }

    public static String getDescription() {
        getDetails();
        return desc;
    }

    public static inputModel[] getInputs() {
        getDetails();
        return inputs;
    }

    public static inputModel getcurrInput() {
        //getDetails();
        Page2.addVis = true;
        Page2.submitVis = false;
        count++;
        

        inputModel test = new inputModel("No inputs for this workflow", "", "", "");
        if (inputs.length == 0) {
            currInput = test;
        } else {
            currInput = inputs[count];
        }
        return currInput;
    }

    public static String getThumb() {
        getDetails();
        return thumb;
    }
}
